<?php
include_once("../function/sql_func.php");
include_once("../connect.php");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
table{width:100%;}
#inp_text {
  /*  border: 1px solid #FFFFFF;*/
    padding: 5px;
    width: 100%;
}
#inp_text2 {
    /*border: 1px solid #FFFFFF;*/
    padding: 5px;
    width: 45%;
}
textarea {
	width:100%;
	height:auto;
   resize: none;
}
</style>

<title>ข้อมูลลูกค้า</title>
</head>
<body>
	<?php
		$table="customer";
    	$field=array(
		'รหัสลูกค้า',
		'ชื่อ - สกุล',
		'เบอร์โทรศัพท์',
		'ที่อยู่',
		'รหัสผู้ใช้งาน',
		'แก้ไข',
		'ลบ',
		);
		//echo end(array_keys($field)); //6
		$th_field=create_field($field);
    ?>
		<table border="1">
        <?php echo $th_field; ?>
        <?php $record=select_all($table);
			while($data=mysql_fetch_array($record))
			{
				if($_GET['cus_id']!=$data[cus_id])
				{
					echo "<tr>";
					echo "<td>".$data[cus_id]."</td>";	
					echo "<td>".$data[cus_name]." ".$data[cus_lname]."</td>";
					echo "<td>".$data[cus_tel]."</td>";
					echo "<td>".$data[cus_add]."</td>";
					echo "<td>".$data[username]."</td>";
					echo "<td><a href='?cus_id=$data[cus_id]'> แก้ไข </a></td>";
					echo"<td><a href='?cus_id=$data[cus_id]&method=delete'> ลบ </a></td>";
					echo "</tr>";
				}
				else
				{
					?> 
                    <form id='frm1' method='get' name='form1' action=''>
					 <tr>
					<td><input type='text' name='txtCus_id' value='<?php echo $data[cus_id];?>' id='inp_text' readonly></td>
					<td><input type='text' name='txtCus_name' value='<?php echo $data[cus_name]; ?>' id='inp_text2'>&nbsp;
                    		<input type='text' name='txtCus_lname' value='<?php echo $data[cus_lname]; ?>' id='inp_text2'>
                     </td>
					<td><input type='text' name='txtCus_tel' value='<?php echo $data[cus_tel];?>'></td>
					<td><textarea name='txtCus_add' ><?php echo $data[cus_add]; ?></textarea></td>
					<td><textarea name='txtCus_user' ><?php echo $data[cus_user]; ?></textarea></td>
                    <td><input type='submit' value="ยืนยัน"></td>
                    <td><input type='reset' value='ล้าง'> </td>
                    
                    <input type='hidden' value='edit' name='method'>
					
					</tr>
                    </form>
					<?php
				}
			}//end while
		?>
        </table>
        
        <?php
		if($_GET['method']=="edit")
		{
			$txtCus_id=$_REQUEST['txtCus_id'];
			$txtCus_name=$_REQUEST['txtCus_name'];
			$txtCus_lname=$_REQUEST['txtCus_lname'];
			$txtCus_tel=$_REQUEST['txtCus_tel'];
			$txtCus_add=$_REQUEST['txtCus_add'];
			$txtCus_user=$_REQUEST['txtCus_user'];
			
			$set_value=array(			
			'cus_name'=>$txtCus_name,
			'cus_lname'=>$txtCus_lname,
			'cus_tedl'=>$txtCus_tel,
			'cus_add'=>$txtCus_add,
			'username'=>$txtCus_user,
			'cus_id'=>$txtCus_id
			);
			
			//$sql="UPDATE customer set ";
			/*foreach($set_value as $value=>$x)
			{
					echo $value."=".$x;
			}*/
			//echo count($set_value);
			
			$result_end=update($set_value,$table);
			//echo $result_end;
			
			if($result_end)
			{
				
				echo "<script>alert(' แก้ไขข้อมูลเรียบร้อย ');window.location='?method=NULL';</script>"; 					
			}
			else
			{
				echo "<script>alert(' ไม่สามารถแก้ไขได้ เกิดข้อผิดพลาด');</script>";	
			}
			//echo $method;
		}
		elseif($_GET['method']=='delete')
		{
			$delCus_id=$_REQUEST['cus_id'];
			
			$delValue=array(
			'cus_id'=>$delCus_id
			);
			
			$result_end=delete($delValue,$table);
			
			if($result_end)
			{
				echo "<script> alert('แก้ไขข้อมูลเรียบร้อย');window.location='?method=NULL';</script>";	
			}
			else
			{
				echo "<script>alert('ไม่สามารถลบได้');</script>	";
			}
		}
		?>
</body>
</html>
